package com.duruikang.LeaveAPI.api;

import com.duruikang.LeaveAPI.common.http.ResponseResult;
import com.duruikang.LeaveAPI.common.sercurity.utils.JwtTokenUtil;
import com.duruikang.LeaveAPI.form.TokenForm;
import com.duruikang.LeaveAPI.service.impl.SecurityServiceImpl;
import com.duruikang.LeaveAPI.service.impl.StudentInformationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author Drik
 * @date 2021-12-17 22:40
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
public class StudentApi {
    /**
     * 服务层对象
     */
    @Autowired
    private StudentInformationServiceImpl ser;

    /**
     * 安全验证服务层对象
     */
    @Autowired
    private SecurityServiceImpl securityService;

    /**
     * 封装token的工具类对象
     */
    @Autowired
    private JwtTokenUtil jwtUtil;
    /**
     * 返回学生的信息
     *
     * @param form
     * @return 学生信息
     */
    @PostMapping("/getstudent")
    public ResponseResult<Map<String, String>> getStudent(@RequestBody @Valid TokenForm form) {
        String checkResult = securityService.checkToken(form.getToken());
        if(checkResult!=null){
            return ResponseResult.getErrorResult(checkResult);
        }
        String accountId = jwtUtil.getUsername(form.getToken());
        Map<String, String> res = ser.getStudent(accountId);
        return ResponseResult.getSuccessResult(res);
    }
}
